import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:livan_app_bak/module/cache_util.dart';
import 'package:livan_app_bak/page/shop_web_view/shop_web_view.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page - '),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final RegExp regexp = RegExp("[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]");
  TextEditingController tec = TextEditingController();
  FocusNode fn = FocusNode();
  String _resData = "";

  void _incrementCounter() {
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              margin: const EdgeInsets.only(left: 16, right: 16, top: 10),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(2),
                border: Border.all(width: 1, color: const Color(0xFFDFDFDD)),
                color: Colors.white,
              ),
              child: TextField(
                buildCounter: (
                    BuildContext context, {
                      required int currentLength,
                      required int? maxLength,
                      required bool isFocused,
                    }) {
                  return Padding(
                    padding: const EdgeInsets.only(bottom: 10),
                    child: Text(
                      "$currentLength/$maxLength",
                      style: const TextStyle(
                        fontSize: 14,
                        color: Color(0xFFAAAFB5),
                      ),
                    ),
                  );
                },
                scrollPadding: EdgeInsets.zero,
                controller: tec,
                focusNode: fn,
                maxLength: 3000,
                maxLines: 8,
                minLines: 1,
                style: const TextStyle(
                  fontSize: 14,
                  color: Color.fromRGBO(102, 105, 109, 1),
                ),
                decoration: InputDecoration(
                  isCollapsed: true,
                  prefix: const Text("密文: "),
                  suffixIcon: TextButton(onPressed: () => decrypt(), child: const Text("解密")),
                  hintStyle: const TextStyle(color: Color(0xFFAAAFB5), fontSize: 14),
                  hintText: "请输入加密内容",
                  contentPadding: const EdgeInsets.only(top: 8, bottom: 5, left: 13, right: 8,),
                  border: const OutlineInputBorder(borderSide: BorderSide.none),
                ),
                inputFormatters: [FilteringTextInputFormatter.deny(regexp)],
              ),
            ),
            Expanded(
              child: Container(
                padding: EdgeInsets.all(40),
                child: Text(_resData),
              ),
            ),
            Offstage(
              offstage: _resData.isEmpty,
              child: Row(
                children: [
                  const Spacer(),
                  TextButton(
                    onPressed: () => resetValue(),
                    child: const Text(
                      "重置",
                      style: TextStyle(
                        color: Colors.black87,
                        fontSize: 14,
                      ),
                    ),
                  ),
                  const SizedBox(width: 40),
                  TextButton(
                    onPressed: () => copyValue(),
                    child: const Text(
                      "复制",
                      style: TextStyle(
                        color: Colors.black87,
                        fontSize: 14,
                      ),
                    ),
                  ),
                  const Spacer(),
                ],
              ),
            ),
            const SizedBox(height: 40),
            TextButton(
              child: Text("打开商城"),
              onPressed: () async {
                Navigator.push(context,  MaterialPageRoute (
                  builder: (BuildContext context) => ShopWebView(),
                ));
              },
            ),
            const SizedBox(height: 40),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void decrypt() {
    final value = tec.text;
    if (value.isEmpty) {
      return;
    }
    final result = CacheUtil.unNiceDay_01(value) ?? "";
    if (result.isEmpty) {
      return;
    }
    setState(() {
      _resData = result;
    });
  }

  void resetValue() {
    setState(() {
      tec.text = '';
      _resData = '';
    });
  }
  void copyValue() {
    Clipboard.setData(ClipboardData(text: _resData));
  }
}
